﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BluePortal.Framework;
using System.Data.Linq;

namespace BluePortal.Medias.Core
{
    public class BCCategory : AbstractBusinessComponent
    {
        #region Internal methods
        public IQueryable<Category> QueryFindByMedia(Guid VideoID)
        {
            IQueryable<Category> var = from c in Context.Categories
                                       from vc in c.MediaCategories
                                       where vc.ID == VideoID
                                       select c;
            return var;
        }

        public IQueryable<Category> QueryFindAllByMedia(Guid VideoID)
        {
            var query = from c in Context.Categories
                        select c;
            foreach (Category c in query)
            {
                var q = from vc in Context.MediaCategories where vc.CID == c.CID && vc.ID == VideoID select vc;
                foreach (MediaCategory vc in q)
                    c.MediaCategories.Add(vc);
            }
            return query;
        }

        public IQueryable<Category> QueryFindByName(string name)
        {
            var query = from c in Context.Categories
                        where c.Name.StartsWith(name)
                        select c;
            return query;
        }

        public IQueryable<Category> QueryFindAll()
        {
            var query = from c in Context.Categories
                        select c;
            return query;
        }
        #endregion

        public void Create(Category category)
        {
            AssertNotNull(category, "Category");
            if ((category.CID == null) || (category.CID == Guid.Empty)) category.CID = System.Guid.NewGuid();
            Context.Categories.InsertOnSubmit(category);
            Context.SubmitChanges();
        }

        public void Delete(Category category)
        {
            AssertNotNull(category, "Category");
            Category cat = Context.Categories.Single<Category>(c => c.CID == category.CID);
            Context.Categories.DeleteOnSubmit(cat);
            Context.SubmitChanges();
        }

        public void Update(Category category)
        {
            AssertNotNull(category, "Category");
            Category cat = Context.Categories.Single<Category>(c => c.CID == category.CID);
            cat.Name = category.Name;
            Context.SubmitChanges();
        }

        public Category FindByID(Guid categoryID)
        {
            return Context.Categories.Single<Category>(c => c.CID == categoryID);
        }

        public void Add(Guid categoryID, Guid VideoID)
        {
            MediaCategory vc = new MediaCategory();
            vc.CID = categoryID;
            vc.ID = VideoID;
            if (Context.MediaCategories.Contains<MediaCategory>(vc))
            {
                Log.LogInfo("CategoryService", "AddCategory2Video",
                    String.Format("Category {0} already added to Video {1}", categoryID, VideoID));
            }
            else
            {
                Context.MediaCategories.InsertOnSubmit(vc);
                Context.SubmitChanges();
            }
        }

        public IEnumerable<Category> FindChildren(Guid cid)
        {
            if (cid != Guid.Empty)
            {
                var query = from c in Context.Categories
                            where c.ParentCategory == cid
                            select c;

                return query;
            }
            else
            {
                var query = from c in Context.Categories
                            where c.ParentCategory == null
                            select c;
                return query;
            }
        }

        public IEnumerable<Category> FindParent(Guid cid)
        {
            Category category = FindByID(cid);
            if (category.ParentCategory.HasValue)
            {

                Guid parent = category.ParentCategory.Value;

                var query = from c in Context.Categories
                            where c.CID == parent
                            select c;
                return query;
            }
            else
                return null;
        }

        public void Remove(Guid categoryID, Guid VideoID)
        {
            MediaCategory res = Context.MediaCategories.Single<MediaCategory>(vc => vc.ID == VideoID & vc.CID == categoryID);
            Context.MediaCategories.DeleteOnSubmit(res);
            Context.SubmitChanges();
        }

        public IEnumerable<Category> FindAll()
        {
            return QueryFindAll().AsEnumerable();
        }

        public IEnumerable<Category> FindByName(string name)
        {
            return QueryFindByName(name).AsEnumerable();
        }

        public IEnumerable<Category> FindAllByMedia(Guid id)
        {
            return QueryFindAllByMedia(id).AsEnumerable();
        }

        public IEnumerable<Category> FindByMedia(Guid id)
        {
            return QueryFindByMedia(id).AsEnumerable();
        }
    }
}
